﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XMK.Tadong.Biz;
using XMK.Framework.Model;
using XMK.Utils;
using System.Data;

namespace XMK.Tadong.Web.Handlers
{
    /// <summary>
    /// StructTableDictHandler 的摘要说明
    /// </summary>
    public class StructTableDictHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string resultJson = "{\"success\":true}";
            string op = context.Request.Params.Get("optype");
            switch (op)
            {
                case "keywsearch":
                    resultJson = SearchKeywData(context);
                    break;
                case "querydata":
                    resultJson = QueryTableData(context);
                    break;
                case "getdict":
                default:
                    resultJson = QueryTableDictByTableCode(context);
                    break;
            }

            context.Response.ContentType = "text/json";
            context.Response.Write(resultJson);
            context.Response.End();
        }

        private string QueryTableDictByTableCode(HttpContext context)
        {
            V_Base_Struct_KeywBiz biz = new V_Base_Struct_KeywBiz();
            ResponseInfo resp = biz.QueryTableColumnList(context.Request.Params.Get("tab_code"));
            return JsonHelper.ToJson(resp.Data);
        }

        private string QueryTableData(HttpContext context)
        {
            PageInfo page = new PageInfo();
            page.PageSize = ParseHelper.Parse<int>(context.Request.Params.Get("rows"));
            page.CurrentPage = ParseHelper.Parse<int>(context.Request.Params.Get("page"));
            OrderInfo order = new OrderInfo();
            order.ColName = "jh";
            order.Sort = "asc";
            V_Base_Struct_KeywBiz biz = new V_Base_Struct_KeywBiz();
            ResponseInfo resp = biz.QueryTableData(context.Request.Params.Get("proc"),
                context.Request.Params.Get("kw"),page, order
                );
            return string.Concat("{\"total\":", resp.Page.Records,", \"rows\":", JsonHelper.ToJson(resp.Data) ,"}");
        }

        private string SearchKeywData(HttpContext context)
        {
            V_Base_Struct_KeywBiz biz = new V_Base_Struct_KeywBiz();
            ResponseInfo resp = biz.SearchList(context.Request.Params.Get("keyw"));
            DataTable table = resp.Data as DataTable;
            if (table == null)
            {
                return "{\"success\":false}";
            }
            var q = from row in table.AsEnumerable() 
                    group row by row["t_cat"].ToString() into g
                    select new { g.Key, en_cat = g.ElementAt(0)["en_t_cat"].ToString(), CatCount = g.Sum(r => int.Parse(r["cnt"].ToString())) };
            DataTable catTable = new DataTable();
            catTable.Columns.Add("t_cat", typeof(string));
            catTable.Columns.Add("en_t_cat", typeof(string));
            catTable.Columns.Add("cnt", typeof(int));
            foreach (var item in q)
            {
                DataRow row = catTable.NewRow();
                row["t_cat"] = item.Key;
                row["en_t_cat"] = item.en_cat;
                row["cnt"] = item.CatCount;
                catTable.Rows.Add(row);
            }
            return string.Concat("{\"success\":true,\"cat\":", JsonHelper.ToJson(catTable), ", \"detail\":", JsonHelper.ToJson(table), "}");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}